window.onload = function() {
    goods_setTransferTable();
}
function goods_setTransferTable() {
    setTransferTable(
            Array("goodsList"),
            Array("Bdragtable"),
            "li",
            goods_createLiNode,
            Array("hidGoodsNo", "hidGoodsName")
    );
    sortable("Bdragtable", "li");
    setSortableStopEvent("Bdragtable",goods_order);
}
function goods_order(){
    li_order("Bdragtable");
}

function goods_up(strLiId){
    li_up(strLiId, "Bdragtable")
}

function goods_down(strLiId){
    li_down(strLiId, "Bdragtable")
}

function goods_deleteGoods(strLiId) {
    li_del(strLiId, "Bdragtable", "goodsList");
    goods_order();
}

function goods_createLiNode(arrValues) {
    var strLi;
    var i = 1;
    objEach("span[id='liNo']", function(k){
        i = k + 2
    });
    i = parseInt(i);
    strLi = " <li id=\"Bdragtable"+ arrValues[0]+ "\" class=\"ui-state-default\" >"+
                 "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"BNoMarginTable\">" +
                     "<tr>" +
                         "<td width=\"51\" align=\"right\" >" + 
                             "<span id=\"liNo\">" + i + "</span>" + 
                             "<input type = \"hidden\" id = \"goodsNo\" value = \"" + arrValues[0] + "\" />" +
                         "</td>" +
                         "<td width=\"140\"><div class=\"Ctxtparagraph\" >" + arrValues[1] + "<div></td>" +
                         "<td width=\"38\"><input type = 'text' id = 'txtNum' name = 'txtNum' size = '1'>" + "</td>" +
                         "<td width=\"60\">" + 
                             "<img src=\"" + getskip_up() + "\" onclick = \"goods_up('Bdragtable" + arrValues[0] + "')\" />" + "&nbsp" +
                             "<img src=\"" + getskip_down() + "\" onclick = \"goods_down('Bdragtable" + arrValues[0] + "')\" />" + 
                         "</td>" +
                         "<td width=\"35\">" +
                             "<img src=\""+ getDeleteImage()+ "\" onclick=\"goods_deleteGoods('Bdragtable" + arrValues[0] + "')\" />"+
                         "</td>" +
                         "<td width=\"24\" class=\"BgoodsLinksright\"></td>"+
                     "</tr>" +
                 "</table>" +
                 "<input type=\"hidden\" id=\"hidGoodsFormulaName" + arrValues[0]+"\" name=\"hidGoodsFormulaName\" value=\"" + arrValues[1] + "\" />"+
                 "<input type=\"hidden\" id=\"hidGoodsFormulaNo" + arrValues[0]+"\" name=\"hidGoodsFormulaNo\" value=\"" + arrValues[0] + "\" />"+
             "</li>";
    return strLi;
}
function goods_setTextType(strfg){
    setValue("hidNumFg",strfg);
    if(strfg == "3") {
        setDisabled("txtNumFrom",false);
        setDisabled("txtNumTo",false);
    } else {
        setDisabled("txtNumFrom",true);
        setDisabled("txtNumTo",true);
    }
}
function goods_reset() {
    clearErrorMsg();
    setValue("txtGoodsCode","");
    setValue("txtGoodsName","");
    setValue("txtPriceFrom","");
    setValue("txtPriceTo","");
    setSelectedByValue("goodsFirstCATG","");
    setDisabled("goodsSecondCATG",true);
    document.getElementById("rdoNumAll").checked = true;
    setValue("txtNumFrom","");
    setValue("txtNumTo","");
}
function goods_dataSearch() {
    clearErrorMsg();
    goods_search(1, "", "", "1");
    setSearchOrder("orderHead");
}
function goods_search(intCurPage, strOrderColumn, strOrder, strFlag, pageSize) {
    var arrFormulaGoodsNo = new Array();
    var arrObjFormulaGoodsNo = document.getElementsByName("hidGoodsFormulaNo");
    for (i = 0;i < arrObjFormulaGoodsNo.length ;i++) {
        arrFormulaGoodsNo[i] = arrObjFormulaGoodsNo[i].value;
    }
    if(strFlag == "1") {
        ajaxRequest(getHost()+"goods/goodsFormula/search", {
            "curPage"       : intCurPage,
            "pageSize"      : pageSize,
            "GoodsCode"     : getValue("txtGoodsCode"),
            "GoodsName"     : getValue("txtGoodsName"),
            "PriceFrom"     : getValue("txtPriceFrom"),
            "PriceTo"       : getValue("txtPriceTo"),
            "FirstCatg"     : getSelectedValue("goodsFirstCATG"),
            "SecondCatg"    : getSelectedValue("goodsSecondCATG"),
            "StockNumFg"    : getValue("hidNumFg"),
            "StockNumFrom"  : getValue("txtNumFrom"),
            "StockNumTo"    : getValue("txtNumTo"),
            "FormulaGoodsNo"    : arrayToJson(arrFormulaGoodsNo),
            "Flag"          : strFlag
        }, goods_goodsListView);
    } else {
        ajaxRequest(getHost()+"goods/goodsFormula/search", {
            "curPage"       : intCurPage,
            "FormulaGoodsNo"    : arrayToJson(arrFormulaGoodsNo),
            "pageSize"      : pageSize,
            "orderColumn"   : strOrderColumn,
            "order"         : strOrder,
            "Flag"          : strFlag
        }, goods_goodsListView);
    }
}

function goods_goodsListView(data) {
    var strLi = "";
    var data_table= "";
    if (data.count > 0) {
        pageCreat(data.curPage, data.count, "pageHead");
        pageCreat(data.curPage, data.count, "pageFoot");
        for(i = 0; data.searchData[i]; i++) {
            strLi = "";
            strLi = strLi + "<li id='goodsList" + data.searchData[i].GOODS_NO + "' >";
            strLi = strLi + "<table width='100%' class='BListTable' >";
            if (data.searchData[i].disableFlag == "1"){
                strLi = strLi + "<tr class = \"BClickmoveGrayoutOdd\" flag=\"1\">";
            }else{
                strLi = strLi + "<tr class = \"BClickmoveOdd\" >";
            }
            strLi = strLi + "<td width=\"10%\" align=\"left\" class=\"title1\">";
            strLi = strLi + "<img id=\"imgDelete\" src=\"" + getHost() + "theme/commonTheme/img/ec-shop_icon1.gif\"/>";
            strLi = strLi + ((data.curPage - 1) * getPageSize() + i +1);
            strLi = strLi + "</td>";
            strLi = strLi + "<td width=\"20%\" align=\"left\" class=\"title1\">";
            strLi = strLi + "<a href=" + getHost()+ "supplier/supplierView/index/SupplierNo/"+ data.searchData[i].SUPPLIER_NO + ">";
            strLi = strLi + data.searchData[i].GOODS_CODE + "</a>";
            strLi = strLi + "</td>";
            strLi = strLi + "<td width=\"30%\" align=\"left\" class=\"title1\"><div class=\"Ctxtparagraphleft\" style=\"height:50px; margin-top:5px;\">";
            strLi = strLi + data.searchData[i].GOODS_NAME;
            strLi = strLi + "</div></td>";
            strLi = strLi + "<td width=\"40%\" align=\"left\" class=\"title1\"><div class=\"Ctxtparagraphleft\" style=\"height:50px; margin-top:5px;\">";
            strLi = strLi + data.searchData[i].STOCK_NUM;
            strLi = strLi + "</div></td>";
            strLi = strLi + "</tr>";
            strLi = strLi + "</table>";
            strLi = strLi + "<input type=\"hidden\" id=\"hidGoodsNo\" ";
            strLi = strLi + "name=\"hidGoodsNo\" value=\"";
            strLi = strLi + data.searchData[i].GOODS_NO+"\" />";
            strLi = strLi + "<input type=\"hidden\" id=\"hidGoodsName\" ";
            strLi = strLi + "name=\"hidGoodsName\" value=\"";
            strLi = strLi + data.searchData[i].GOODS_NAME+"\" />";
            strLi = strLi + "</li>";
            data_table = data_table + strLi;
        }
        setInnerHTML("goodsList", data_table);
        goods_setTransferTable();
    }else {
        setInnerHTML("goodsList", data_table);
        pageCreat(data.curPage, data.count, "pageHead");
        pageCreat(data.curPage, data.count, "pageFoot");
        var arrErrorMsg = new Array();
        arrErrorMsg[arrErrorMsg.length] = getMessage("ERR-100002");
        clearErrorMsg();
        setErrorMsg(arrErrorMsg,600);
    }
}
function goods_FormulaSaveClick(strAddEditFg) {
    var arrFormulaGoodsName = new Array();
    var arrFormulaGoodsNo = new Array();
    var arrFormulaGoodsNum = new Array();
    var arrObjFormulaGoodsName = document.getElementsByName("hidGoodsFormulaName");
    var arrObjFormulaGoodsNo = document.getElementsByName("hidGoodsFormulaNo");
    var arrObjFormulaGoodsNum = document.getElementsByName("txtNum");
    for (i = 0;i < arrObjFormulaGoodsName.length ;i++) {
        arrFormulaGoodsName[i] = arrObjFormulaGoodsName[i].value;
    }
    for (i = 0;i < arrObjFormulaGoodsNo.length ;i++) {
        arrFormulaGoodsNo[i] = arrObjFormulaGoodsNo[i].value;
    }
    for (i = 0;i < arrObjFormulaGoodsNum.length ;i++) {
        arrFormulaGoodsNum[i] = arrObjFormulaGoodsNum[i].value;
    }
    ajaxRequest(getHost()+"goods/goodsFormula/save", {
        "AddEditFg"             : strAddEditFg,
        "GoodsNo"               : getValue("hidGoodsNo"),
        "FormulaNo"             : getValue("hidFormulaNo"),
        "GoodsTp"               : getSelectedValue("selGoodsTp"),
        "FormulaName"           : getValue("txtFormulaName"),
        "ProductName"           : getValue("txtProductName"),
        "ProductCode"           : getValue("txtProductCode"),
        "FormulaGoodsName"      : arrayToJson(arrFormulaGoodsName),
        "FormulaGoodsNo"        : arrayToJson(arrFormulaGoodsNo),
        "FormulaGoodsNum"       : arrayToJson(arrFormulaGoodsNum)
    }, goods_GoodsFormulaSaveCallback);
}
function goods_GoodsFormulaSaveCallback() {
    if(getValue("hidaddEditFg") == 1) {
        document.goodsFormulaForm.action = getHost() + "goods/goodsFormulaList/index/SaveFg/1";
        document.goodsFormulaForm.submit();
    } else {
        strFormulaNo = getValue("hidFormulaNo");
        document.goodsFormulaForm.action = getHost() + "goods/goodsFormulaDetail/index/formulaNo/" + strFormulaNo;
        document.goodsFormulaForm.submit();
    }
}
function goods_back() {
    if(getValue("hidaddEditFg") == 1) {
        document.goodsFormulaForm.action = getHost() + "goods/goodsFormulaList/index";
        document.goodsFormulaForm.submit();
    } else {
        strFormulaNo = getValue("hidFormulaNo");
        document.goodsFormulaForm.action = getHost() + "goods/goodsFormulaDetail/index/formulaNo/" + strFormulaNo;
        document.goodsFormulaForm.submit();
    }
}
function gotoPage(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function firstPage(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function prePage(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function nextPage(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function lastPage(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function orderSearch(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}
function sizeChangeSearch(strPageContainerId, strOrderContainerId) {
    goods_search(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}